<?
function list_catalogues($catalogues, $title){
    foreach($catalogues as $catalogue){
        $selected = ($catalogue['name'] == $title) ? 'SELECTED' : '';
        echo "<option $selected value=\"{$catalogue['id']}\">{$catalogue['name']}</option>";
        if(isset($catalogue['subcatalogues']))
            list_catalogues($catalogue['subcatalogues'], $title);
    }
}
?>

<div id="products_block">
    <div id="filter">
        <select name="catalogue_id" style="width:220px">
            <option value="">Выберите каталог</option>
            <?
                list_catalogues($catalogues, $title);
            ?>
        </select>
        <select name="sex" style="width:160px">
            <option value="">Выберите пол</option>
            <?
                foreach($sex as $value){
                    echo "<option value=\"$value\">$value</option>";
                }
            ?>
        </select>
        
        <select name="price" style="width:180px">
            <option value="">Выберите цену</option>
            <option value="50-1000">50-1000</option>
            <option value="1000-2000">1000-2000</option>
            <option value="2000-5000">2000-5000</option>
            <option value="5000-10000">5000-10000</option>
        </select>
    </div>
    <div id="products">
        <?= View::factory('catalogue/product_items')
                 ->set('products', $products)
                 ->render(); ?>
    </div>
</div>

<?= View::factory('catalogue/cart')->render() ?>

<script type="text/javascript">
    function on_select_change(){
        var data = {};
        $('#filter select').each(function(){
            var key = $(this).attr('name');
            var value = $(this).attr('value');
            if(value.length)
                data[key] = value;                  
        });
        if(data['catalogue_id']){
            $.ajax({'url': '/catalogue/filter',
                    'type': 'POST',
                    'data': data,
                    'timeout': 5000,
                    'success': function(data){
                        $('#products').html(data);
                        $('.draggable').draggable({'helper': 'clone',
                                     'revert': 'invalid',
                                     'opacity': 0.7,
                                     'revertDuration': 300,
                                     'zIndex': 100,
                                     'scope': 'cart'});
                    },
                    'error': function(){
                        alert('Ошибка обращения к серверу');
                    }                   
            });
        }

        
        
    }
    
    $(document).ready(function(){
        on_select_change();
        
        $('#filter select').bind('change', function(){
            on_select_change();
        })
    });
</script>